package com.snake.springboot.authority.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.snake.springboot.authority.api.model.AuthInterfaceModel;
import com.snake.springboot.authority.api.model.AuthRuleModel;
import com.snake.springboot.authority.api.request.AuthInterfaceQuery;
import com.snake.springboot.authority.api.service.AuthInterfaceService;
import com.snake.springboot.authority.dao.mybatis.entity.AuthInterfaceDO;
import com.snake.springboot.authority.dao.mybatis.entity.AuthRuleDO;
import com.snake.springboot.authority.dao.mybatis.mappers.AuthInterfaceMapper;
import com.snake.springboot.authority.service.assembler.AuthInterfaceAssembler;
import org.snake.common.result.ReturnList;
import org.snake.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类 {@code AuthInterfaceServiceImpl}  <br> .
 * <p>服务请求接口服务实现类
 * 创建时间：2024/5/27 11:51
 *
 * @author Yzhui
 * @see
 */
@Service
public class AuthInterfaceServiceImpl implements AuthInterfaceService {

    private AuthInterfaceMapper authInterfaceMapper;

    @Autowired
    public AuthInterfaceServiceImpl(AuthInterfaceMapper authInterfaceMapper) {
        this.authInterfaceMapper = authInterfaceMapper;
    }

    @Override
    public AuthInterfaceModel addInterface(AuthInterfaceModel model) throws Exception {
        AuthInterfaceDO entity = AuthInterfaceAssembler.assembleAuthInterfaceDO(model, true);
        authInterfaceMapper.insert(entity);
        model.setInterfaceId(entity.getInterfaceId());
        return model;
    }

    @Override
    public int modifyInterface(AuthInterfaceModel model) throws Exception {
        AuthInterfaceDO entity = AuthInterfaceAssembler.assembleAuthInterfaceDO(model, false);
        return authInterfaceMapper.update(entity);
    }

    @Override
    public int removeInterface(String interfaceId) {
        return authInterfaceMapper.delete(interfaceId);
    }

    @Override
    public AuthInterfaceModel querySingleInterface(String interfaceId) {
        AuthInterfaceDO entity = authInterfaceMapper.findById(interfaceId);
        AuthInterfaceModel model = AuthInterfaceAssembler.assembleAuthInterfaceModel(entity);
        return model;
    }

    @Override
    public ReturnList<AuthInterfaceModel> queryInterfaceList(AuthInterfaceQuery params) throws Exception {
        AuthInterfaceDO condition = AuthInterfaceAssembler.assembleAuthInterfaceDO(params.getParams(), false);
        Page<?> page = PageHelper.startPage(params.getPage().getPageNum(), params.getPage().getPageSize());
        List<AuthInterfaceDO> list = authInterfaceMapper.findByCondition(condition);
        List<AuthInterfaceModel> rspList = ListUtils.copy(list, AuthInterfaceModel.class);
        ReturnList<AuthInterfaceModel> result = new ReturnList<>(rspList, page.getTotal());
        return result;
    }

    @Override
    public List<AuthRuleModel> queryAuthRuleList(AuthInterfaceModel params) throws Exception {
        AuthInterfaceDO condition = AuthInterfaceAssembler.assembleAuthInterfaceDO(params, false);
        List<AuthRuleDO> list = authInterfaceMapper.findRuleList(condition);
        List<AuthRuleModel> result = ListUtils.copy(list, AuthRuleModel.class);
        return result;
    }
}
